﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure

Namespace UserAccountWrappers

    Public NotInheritable Class MembershipHelper
        Private Shared userPrivilegesRepositoryValue As IUserPrivilegesRepository

        Private Sub New()
        End Sub
        Public Shared ReadOnly Property UserName() As String
            Get
                Dim ticketValue As String = Nothing

                Dim cookie = HttpContext.Current.Request.Cookies(FormsAuthentication.FormsCookieName)
                If cookie IsNot Nothing Then
                    ticketValue = cookie.Value
                End If

                If Not String.IsNullOrEmpty(ticketValue) Then
                    Dim ticket As FormsAuthenticationTicket

                    Try
                        ticket = FormsAuthentication.Decrypt(ticketValue)
                    Catch
                        Return String.Empty
                    End Try

                    If ticket IsNot Nothing Then
                        Dim identity = New FormsIdentity(ticket)

                        Return identity.Name
                    Else
                        Return String.Empty
                    End If
                Else
                    Return String.Empty
                End If
            End Get
        End Property

        Private Shared ReadOnly Property UserPrivilegesRepository() As IUserPrivilegesRepository
            Get
                If userPrivilegesRepositoryValue Is Nothing Then
#If SQLONLY Then
                    userPrivilegesRepositoryValue = New SqlDataContext()
#Else
                    userPrivilegesRepositoryValue = New UserTablesServiceContext()
#End If
                End If
                Return userPrivilegesRepositoryValue
            End Get
        End Property

        Public Shared Function IsUserAdmin() As Boolean
            Return IsUserLoggedIn() AndAlso UserPrivilegesRepository.HasUserPrivilege(Membership.GetUser(UserName).ProviderUserKey.ToString(), PrivilegeConstants.AdminPrivilege)
        End Function

        Public Shared Function IsUserLoggedIn() As Boolean
            Return Not String.IsNullOrWhiteSpace(UserName)
        End Function
    End Class
End Namespace